"""Adds additional project specific configuration items.

Revision ID: f95247c63eda
Revises: d1dc160533c7
Create Date: 2021-10-28 13:44:55.105570

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
from dispatch.config import Config

# revision identifiers, used by Alembic.
revision = "f95247c63eda"
down_revision = "d1dc160533c7"
branch_labels = None
depends_on = None

config = Config(".env")
Base = declarative_base()


class Project(Base):
    __tablename__ = "project"
    id = Column(Integer, primary_key=True)
    annual_employee_cost = Column(Integer, default=650000)
    business_year_hours = Column(Integer, default=2000)

    owner_email = Column(String)
    owner_conversation = Column(String)


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("project", sa.Column("annual_employee_cost", sa.Integer(), nullable=True))
    op.add_column("project", sa.Column("business_year_hours", sa.Integer(), nullable=True))
    op.add_column("project", sa.Column("owner_email", sa.String(), nullable=True))
    op.add_column("project", sa.Column("owner_conversation", sa.String(), nullable=True))

    bind = op.get_bind()
    session = Session(bind=bind)

    projects = session.query(Project).all()
    for project in projects:
        project.annual_employee_cost = config("ANNUAL_COST_EMPLOYEE", cast=int, default="650000")
        project.business_year_hours = config("BUSINESS_HOURS_YEAR", cast=int, default="2080")
        project.owner_email = config("INCIDENT_RESPONSE_TEAM_EMAIL", default="")

    session.commit()
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("project", "owner_conversation")
    op.drop_column("project", "owner_email")
    op.drop_column("project", "business_year_hours")
    op.drop_column("project", "annual_employee_cost")
    # ### end Alembic commands ###
